var apireadyMap = [memberUinfo];

var ext = {};

function initPage() {
	
    // 绑定查询事件
    $('#submitDatabase').on('click', function() {
        // 记录时间
        var start_date = $('#ipt-start_date').val();
        var finsh_date = $('#ipt-finsh_date').val();
        ext['cpr_start_date'] = start_date;
        updateExt();
        statistics.init();
    });

    var start_date = ext.cpr_start_date || (tool.formatDate('yy-mm-dd') + ' 00:00:00');
	var time = tool.getSearch('time');
	if (time) {
		start_date = tool.formatDate(Number(time)); 
	}
	
    // 初始化时间控件
    laydate.render({
        elem: '#ipt-start_date',
        value: start_date,
        min: '2017-12-24 00:00:00',
        max: tool.formatDate('yy-mm-dd hh:ii:ss'),
        type: 'datetime'
    });

    var finsh_date = tool.formatDate('yy-mm-dd hh:ii:ss');
    // 初始化时间控件
    laydate.render({
        elem: '#ipt-finsh_date',
        value: finsh_date,
        min: '2017-12-24 00:00:00',
        max: tool.formatDate('yy-mm-dd hh:ii:ss'),
        type: 'datetime'
    });

    // 获取数据
    //statistics.init();
    $('#submitDatabase').trigger('click');
}

// 获取用户信息
function memberUinfo() {
    ajax.get('/api?member.uinfo', function(res) {
        if(res.state) {
            try {
                ext = JSON.parse(res.data.ext);
            } catch(e) {
                //TODO handle the exception
                ext = {};
            }
            initPage();
        } else {
            setTimeout(memberUinfo, 1600);
        }
    });
}

// 更新ext
function updateExt() {
    var data_opt = {
        ext: JSON.stringify(ext)
    };
    ajax.put('/api?member.upUinfo', data_opt, function(res) {
        if(res.state) {

        } else {
            setTimeout(updateExt, 1600);
        }
    });
}

// 今日统计
var statistics = {
    repeat: {
        index: 1,
        max: 3
    },
    init: function() {
        var self = statistics;
        var data_opt = {
            start_date: $('#ipt-start_date').val(),
            finsh_date: $('#ipt-finsh_date').val()
        };
        var modalId = modal.load.show();
        ajax.get('/api?ctrl.pk10.planReport', data_opt, function(res) {
            modal.load.hide(modalId);
            if(res.state) {
                self.render(res.data);
            } else {
                if(self.repeat.index <= self.repeat.max) {
                    ++self.repeat.index;
                    // 获取失败，从新再来
                    self.init();
                } else {
                    self.repeat.index = 1;
                }
            }
        });
    },
    render: function(data) {
        var self = statistics;
        var opt = {
            'v1': '冠军',
            'v2': '亚军',
            'v3': '第三位',
            'v4': '第四位',
            'v5': '第五位'
        };
        var ele = '';
        ele += '<table>';
        ele += '<tr>';
        ele += '<td>位</td>';
        ele += '<td>3码</td>';
        ele += '<td>4码</td>';
        ele += '<td>5码</td>';
        ele += '<td>6码</td>';
        ele += '</tr>';
        for(var i = 1; i <= 5; i++) {
            var v = 'v' + i;
            var list = data[v];
            ele += '<tr style="color:#FF5500;">';
            ele += '<td>' + opt[v] + '</td>';
            for(var j = 3; j <= 6; j++) {
                ele += '<td>';
                ele += '<span style="padding-bottom:2px; border-bottom:1px solid #ff8142;">';
                ele += '<font style="color:#1AAD19">' + list['c' + j].win + '</font>';
                ele += '<font style="color:#ff8142"> | </font>';
                ele += '<font style="color:#C91723">' + list['c' + j].mis + '</font>';
                ele += '</span>';
                var count = Number(list['c' + j].win) - Number(list['c' + j].mis);
                if(count >= 0) {
                    ele += '<p style="padding-top:2px;color:#1AAD19;line-height:initial;">+' + count + '</p>';
                } else {
                    ele += '<p style="padding-top:2px;color:#C91723;line-height:initial;">' + count + '</p>';
                }
                ele += '</td>';
            }
            ele += '</tr>';
        }
        // 计算总数
        ele += '<tr style="color:#FF5500;">';
        ele += '<td>总记录</td>';
        for(var j = 3; j <= 6; j++) {
            var c = 'c' + j;
            var win = 0;
            var mis = 0;
            for(var i = 1; i <= 5; i++) {
                var v = 'v' + i;
                win += Number(data[v][c].win);
                mis += Number(data[v][c].mis);
            }
            ele += '<td>';
            ele += '<span style="padding-bottom:2px; border-bottom:1px solid #ff8142;">';
            ele += '<font style="color:#1AAD19">' + win + '</font>';
            ele += '<font style="color:#ff8142"> | </font>';
            ele += '<font style="color:#C91723">' + mis + '</font>';
            ele += '</span>';
            var count = win - mis;
            if(count >= 0) {
                ele += '<p style="padding-top:2px;color:#1AAD19;line-height:initial;">+' + count + '</p>';
            } else {
                ele += '<p style="padding-top:2px;color:#C91723;line-height:initial;">' + count + '</p>';
            }
            ele += '</td>';
        }
        ele += '</tr>';
        ele += '</table>';
        /*console.dir(ele);
        layer.open({
            type: 1,
            shade: 'background-color:rgba(255, 255, 255, 0)',
            content: ele,
            anim: 'scale',
            className: 'popuoModal'
        });*/
        document.querySelector('.container').innerHTML = ele;
        // 收起开奖结果
        var isup = $('.btnToggleBox').hasClass('up');
        if(isup) {
            $('.btnToggle').trigger('click');
        }
    },
    calcPW: function(num, win) {
        if(Number(num) == 0) {
            return 0;
        } else {
            return(Number(win) / Number(num) * 100).toFixed(2);
        }
    },
    calcLost: function(num, win) {
        return Number(num) - Number(win);
    }
};